﻿/**
 * Proyecto: Cognitum
 * Global IT Services 2008
 * @author
 */
Cognitum.Polizas.Siniestro.Beneficios.Componentes.Generales = Ext.extend(Karma.FB2, {

    divisionOperativaBeneficios: null,

    initComponent: function() {
        Ext.apply(this, {
            title: 'Datos generales',
            column1items: [{
                fieldLabel: 'Numero',
                name: 'Numero'
            },
            {
                fieldLabel: 'Reporte',
                name: 'NumeroReporte'
            }, {
                fieldLabel: 'IsComplemento',
                name: 'IsComplemento',
                hidden:true
            }, {
                fieldLabel: 'Fecha Siniestro',
                name: 'FechaSiniestro',
                xtype: 'datefield',
                allowBlank: false,
                disabled: false,
                format: 'd/m/Y H:i:s'
            }, {
                fieldLabel: 'Fecha Reporte',
                name: 'FechaReporte',
                xtype: 'datefield',
                allowBlank: false,
                disabled: false,
                format: 'd/m/Y H:i:s'
            }, {
                fieldLabel: 'F. Recep. Cia.',
                name: 'FechaRecepcionCia',
                xtype: 'datefield',
                allowBlank: false,
                disabled: false,
                format: 'd/m/Y H:i:s'
            }, {
                fieldLabel: 'Estatus',
                name: 'EstatusSiniestro',
                xtype: 'enum.combo',
                enumName: 'Cognitum.Dominio.Polizas.EstatusSiniestroEnum'
            }, {
                fieldLabel: 'Estatus',
                name: 'EstatusPagoSiniestro',
                xtype: 'enum.combo',
                enumName: 'Cognitum.Dominio.Polizas.EstatusPagoSiniestroEnum'
            }, {
                entityName: 'Polizas',
                xtype: 'entity.link',
                fieldLabel: 'Poliza',
                name: 'Poliza',
                readonly: true,
                preContext: [{
                    value: 0,
                    query: 'p.Ramo.DivisionOperativa = ' + this.divisionOperativaBeneficios
}]
                }, {
                    fieldLabel: 'Estatus Cobranza',
                    name: 'Poliza.EstatusCobranza',
                    xtype: 'enum.combo',
                    enumName: 'Cognitum.Dominio.Polizas.EstatusCobranzaEnum',
                    disabled: true
                }, {
                    entityName: 'Certificados',
                    xtype: 'entity.link',
                    fieldLabel: 'Certificado',
                    name: 'BienAsegurado',
                    contextTriggers: [{
                        id: 'Poliza',
                        query: 'm.Poliza.Id=?'
}]
                    }, {
                        fieldLabel: 'Tipo Siniestro',
                        name: 'TipoSiniestro',
                        xtype: 'enum.combo',
                        enumName: 'Cognitum.Dominio.Polizas.Beneficios.TipoSiniestroBeneficiosEnum'
                    }, {
                        fieldLabel: 'Especialidad',
                        name: 'Especialidad',
                        xtype: 'enum.combo',
                        enumName: 'Cognitum.Dominio.Polizas.Beneficios.EspecialidadEnum'
                    }, {
                        fieldLabel: 'Padecimiento',
                        name: 'Padecimiento',
                        xtype: 'textarea',
                        allowblank: false
                    }, {
                        fieldLabel: 'Contacto',
                        name: 'Contacto',
                        entityName: 'ContactoSimple',
                        xtype: 'entity.link'
                    }
        ],
                    column2items: [
                    {
                        fieldLabel: 'Tipo Reclamacion',
                        name: 'TipoReclamacion',
                        xtype: 'enum.combo',
                        enumName: 'Cognitum.Dominio.Polizas.Beneficios.TipoReclamacionEnum'
                    },
             {
                 fieldLabel: 'Total Reclamado',
                 name: 'TotalReclamado',
                 xtype: 'numberfield',
                 vtype: 'numericMask',
                 enableKeyEvents: true,
                 listeners: {
                     'keyup': {
                         fn: this.onKeyUp2,
                         scope: this
                     }
                 }
             }, {
                 fieldLabel: 'Gastos No Cubiertos',
                 name: 'GastosNoCubiertos',
                 xtype: 'numberfield',
                 vtype: 'numericMask',
                 enableKeyEvents: true,
                 listeners: {
                     'keyup': {
                         fn: this.onKeyUp2,
                         scope: this
                     }
                 }
             }, {
                 fieldLabel: 'Deducible %',
                 name: 'DeduciblePct',
                 xtype: 'numberfield',
                 vtype: 'numericMask',
                 enableKeyEvents: true,
                 listeners: {
                     'keyup': {
                         fn: this.onKeyUp2,
                         scope: this
                     }
                 }
             }, {
                 fieldLabel: 'Deducible',
                 name: 'Deducible',
                 xtype: 'numberfield',
                 vtype: 'numericMask',
                 enableKeyEvents: true,
                 listeners: {
                     'keyup': {
                         fn: this.onKeyUp2,
                         scope: this
                     }
                 }
             }, {
                 fieldLabel: 'Coaseguro %',
                 name: 'CoaseguroPct',
                 xtype: 'numberfield',
                 vtype: 'numericMask',
                 enableKeyEvents: true,
                 listeners: {
                     'keyup': {
                         fn: this.onKeyUp2,
                         scope: this
                     }
                 }

             }, {
                 fieldLabel: 'Coaseguro',
                 name: 'Coaseguro',
                 xtype: 'numberfield',
                 vtype: 'numericMask',
                 enableKeyEvents: true,
                 listeners: {
                     'keyup': {
                         fn: this.onKeyUp2,
                         scope: this
                     }
                 }
             }, {
                 fieldLabel: 'Total a Pagar',
                 name: 'TotalAPagar',
                 readOnly: true
             }, {
                 fieldLabel: 'Total Anticipado',
                 name: 'TotalAnticipado',
                 disabled: true
             }, {
                 fieldLabel: 'Total Pagado',
                 name: 'TotalPagado',
                 disabled: true
             }, {
                 fieldLabel: 'Monto Restante',
                 name: 'MontoRestante',
                 disabled: true
}]
                });
                Cognitum.Polizas.Siniestro.Beneficios.Componentes.Generales.superclass.initComponent.apply(this, arguments);
            },

            onLoad: function(value) {
                var poliza = this.getForm().findField('Poliza');
                if (!Ext.isEmpty(value.Poliza)) {
                    poliza.disable();
                }
                this.calculaMonto(null);
            },

            onKeyUp2: function(field, e) {
                this.calculaMonto(field);
            },

            calculaMonto: function(field) {
                var TotalAPagar = this.getForm().findField('TotalAPagar');
                var Coaseguro = this.getForm().findField('Coaseguro');
                var CoaseguroPct = this.getForm().findField('CoaseguroPct');
                var Deducible = this.getForm().findField('Deducible');
                var DeduciblePct = this.getForm().findField('DeduciblePct');
                var TotalReclamado = this.getForm().findField('TotalReclamado');
                var GastosNoCubiertos = this.getForm().findField('GastosNoCubiertos');
                var TotalAnticipado = this.getForm().findField('TotalAnticipado');
                var MontoRestante = this.getForm().findField('MontoRestante');
                var TotalPagado = this.getForm().findField('TotalPagado');
                var subTotal = TotalReclamado.getValue() - GastosNoCubiertos.getValue();

                if (!Ext.isEmpty(field) && field.getName() == 'Coaseguro') {
                    var valor = (parseFloat(Coaseguro.getValue()) * 100) / parseFloat(subTotal);
                    if (valor == 'NaN') {
                        CoaseguroPct.setValue('');
                    }
                    else {
                        CoaseguroPct.setValue(valor);
                    }
                }
                else {
                    var valor = ((parseFloat(CoaseguroPct.getValue()) / 100) * parseFloat(subTotal));
                    if (valor == 'NaN') {
                        Coaseguro.setValue('');
                    }
                    else {
                        Coaseguro.setValue(valor);
                    }
                }

                if (!Ext.isEmpty(field) && field.getName() == 'Deducible') {
                    var valor = (parseFloat(Deducible.getValue()) * 100) / parseFloat(subTotal);
                    if (valor == 'NaN') {
                        DeduciblePct.setValue('');
                    }
                    else {
                        DeduciblePct.setValue(valor);
                    }
                }
                else {
                    var valor = ((parseFloat(DeduciblePct.getValue()) / 100) * parseFloat(subTotal));
                    if (valor == 'NaN') {
                        Deducible.setValue('');
                    }
                    else {
                        Deducible.setValue(valor);
                    }
                }
                TotalAPagar.setValue(subTotal - Deducible.getValue() - Coaseguro.getValue() - TotalAnticipado.getValue());
                MontoRestante.setValue(TotalAPagar.getValue() - TotalPagado.getValue());
            }

        });
Ext.reg('sin.benef.ctls.gral', Cognitum.Polizas.Siniestro.Beneficios.Componentes.Generales);

Cognitum.Polizas.Siniestro.Beneficios.Editor = Ext.extend(Cognitum.Polizas.Siniestro.Editor, {

    divisionOperativaBeneficios: null,
    
    initComponent: function(){
        var _sections = [{
            xtype: 'sin.benef.ctls.gral',
            title: 'General',
            divisionOperativaBeneficios: this.divisionOperativaBeneficios
        }, {
            title: 'Bitacora',
            items: [{
                hideLabel: true,
                name: 'Bitacora',
                entityName: 'BitacoraSiniestro',
                xtype: 'ag.list',
                context: 'b.Siniestro.Id=? or b.Siniestro.Id in (select c.Id from ComplementoSiniestro c where c.Siniestro.Id = ?)',
                anchor: '99% 99%'
            }]
        }, {
            title: 'Complementos',
            items: [{
                hideLabel: true,
                name: 'Complementos',
                entityName: 'ComplementoBeneficios',
                xtype: 'ag.list',
                context: 'c.Siniestro.Id=?',
                anchor: '99% 99%'
            }]
        }, {
            title: 'Pagos y Anticipos',
            items: [{
                hideLabel: true,
                name: 'Pagos',
                entityName: 'PagoBeneficio',
                xtype: 'ag.list',
                context: 'p.Siniestro.Id=?',
                anchor: '99% 99%',
                listeners: {
                    refresh: {
                        fn: function() {
                            this.entityId = this.value.Id;
                            this.loadAction();
                        },
                        scope: this
                    }
                }
            }]
        }, {
            title: 'Documentos',
            items: [{
                fieldLabel: 'Documentos',
                hideLabel: true,
                name: 'Documentos',
                entityName: 'DocumentoDigitalizadoSiniestro',
                xtype: 'ag.list',
                context: 'd.Siniestro.Id=?',
                anchor: '99% 99%'
                }]
            }, {
            xtype: 'aud.editor',
            title: 'Auditoria'
        }];
        
        if (this.seccionesAdicionales) {
            Ext.each(this.seccionesAdicionales, function(item){
                _sections.push(item);
            }, this);
        }
        Ext.apply(this, {
            sections: _sections
        });
        Cognitum.Polizas.Siniestro.Beneficios.Editor.superclass.initComponent.apply(this, arguments);
    }
});

Ext.reg('sin.benef.editor', Cognitum.Polizas.Siniestro.Beneficios.Editor);
